home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks95 / Boom! '94.sit / Boom! '94 / ShowIconFamily.c < prev    next >
Text File  |  1995-06-24  |  9KB  |  324 lines

  1. /*
  2.     ShowIconFamily.c
  3.     
  4.     ShowINIT compatible routine that shows 'ICN#' and 'iclx' flavor icons.
  5.     For use by all INITs in System 7 and beyond.
  6.     
  7.     by Patrick C. Beard.
  8.     
  9.     Instructions for use:
  10.     
  11.         • Create a family of icons with ResEdit 2.1 or later.  This will include
  12.          'ICN#', 'icl4', & 'icl8' icons.
  13.         • Use SetUpA4 to set up Think C globals.
  14.         • Call ShowIconFamily() with the resource id of the family that you used.
  15.     
  16.     Enhancements:
  17.         
  18.         • Uses 'iclx' & 'ICN#' icons from the Finder's "icon family" in System 7.
  19.         • Generates a position for icons that is guaranteed to be on screen, while
  20.           remaining compatible with previous releases of ShowInit.
  21.     
  22.     This code is completely public domain.  Let's hope this becomes a new standard.
  23.     
  24.     This code is derived from the original ShowInit by Paul Mercer, Darin Adler,
  25.     Paul Snively, and Steve Capps.
  26.     
  27.     Special thanks to Ben Haller & Rob Vaterlaus for valuable suggestions and help.
  28.     
  29.     Converted to MetroWerks DR3 @MacHack94 -bh
  30.  */
  31.  
  32. #include <OSUtils.h>
  33. #include <QuickDraw.h>
  34. #include <Memory.h>
  35. #include <Resources.h>
  36.  
  37. #ifndef nil
  38. #define nil ((void*)0)
  39. #endif
  40. #ifndef topLeft
  41. #define topLeft(r) ((Point*)&r)[0])
  42. #endif
  43. #ifndef botRight
  44. #define botRight(r) ((Point*)&r)[1])
  45. #endif
  46.  
  47. struct xQDGlobals {
  48.     char privates[76];
  49.     long randSeed;
  50.     BitMap screenBits;
  51.     Cursor arrow;
  52.     Pattern dkGray;
  53.     Pattern ltGray;
  54.     Pattern gray;
  55.     Pattern black;
  56.     Pattern white;
  57.     GrafPtr thePort;
  58.     long    end;
  59. };
  60.  
  61. typedef struct xQDGlobals xQDGlobals;
  62.  
  63. static short theDepth;                /* the depth the monitor is in. */
  64. static CGrafPtr port;
  65.  
  66. static void DrawBWIcon(short iconId);
  67. static void DrawColorIcon(short iconId);
  68.  
  69. typedef BitMap    *BitMapPtr;
  70.  
  71. //**********************************************************
  72. //                                            protos
  73. void ShowIconFamily(short iconId);
  74. void GetIconRect(register Rect* iconRect);
  75. void DrawBWIcon(short iconId);
  76. Handle ChooseIcon(short iconId, short* suggestedDepth);
  77. void DrawColorIcon(short iconId);
  78.  
  79. /* this is where it all happens. */
  80. void ShowIconFamily(short iconId)
  81. {
  82.     long oldA5;
  83.     xQDGlobals qd;                /* our QD globals. */
  84.     SysEnvRec environment;        /* machine configuration. */
  85.     CGrafPort gp;                /* our grafport. */
  86.     
  87.     /* get a value for A5, a structure that mirrors qd globals. */
  88.     oldA5 = SetA5((long)&qd.end);
  89.     InitGraf(&qd.thePort);
  90.     
  91.     /* find out what kind of machine this is. */
  92.     SysEnvirons(curSysEnvVers, &environment);
  93.     if (environment.hasColorQD) {
  94.         theDepth = (**(**GetMainDevice()).gdPMap).pixelSize;
  95.         if (theDepth < 4)
  96.             theDepth = 1;
  97.     } else {
  98.         theDepth = 1;
  99.     }
  100.  
  101.     /* see what type of port to open. */
  102.     if (theDepth >= 4) {
  103.         OpenCPort(&gp);
  104.     } else {
  105.         OpenPort((GrafPtr)&gp);
  106.     }
  107.     port = &gp;
  108.     
  109.     if (theDepth == 1)
  110.         DrawBWIcon(iconId);
  111.     else
  112.         DrawColorIcon(iconId);
  113.     
  114.     SetA5(oldA5);
  115. }
  116.  
  117. /*
  118.     ShowInit's information is nestled at the tail end of CurApName.
  119.     It consists of a short which encodes the next horizontal offset,
  120.     and another short which is that value checksummed with the function below.
  121.  */
  122.  
  123. #define CurApName_LM    0x910
  124. #define ShowINITTable ((short*)(CurApName_LM + 32 - 4))
  125. #define CheckSumConst 0x1021        /* magic value to check-sum with. */
  126.  
  127. #define InitialXPosition 8            /* initial horizontal offset. */
  128. #define YOffset            40            /* constant from bottom to place the icon. */
  129. #define XOffset            40            /* amount to change it by. */
  130.  
  131. /* CheckSum() computes the magic value to determine if ShowInit's have run already. */
  132.  
  133. static short CheckSum(register short x)
  134. {
  135. #ifndef __MWERKS__
  136.     asm {
  137.         rol.w    #1, x
  138.     }
  139.     return (x ^ CheckSumConst);
  140. #else
  141.     unsigned short t = x & 0x8000;
  142.     x = (x << 1) | (t>>15);
  143.     return (x ^ CheckSumConst);
  144. #endif
  145. }
  146.  
  147. /*
  148.     GetIconRect() generates an appropriate rectangle to display the
  149.     next INIT's icon in.  It is also responsible for updating the horizontal
  150.     position in low memory.  This is a departure from the original ShowInit code,
  151.     which updates low memory AFTER displaying the icon.  This code won't generate
  152.     an icon position until it is certain that the icon can be loaded, so the
  153.     same behaviour occurs.
  154.     
  155.     This routine also generates a rectangle which is guaranteed to be onscreen.  It
  156.     does this by taking the horizontal offset modulo the screen width to generate
  157.     the horizontal position of the icon, and the offset divided by the screen
  158.     width to generate the proper row.
  159.  */
  160. void GetIconRect(register Rect* iconRect)
  161. {
  162.     register short screenWidth = port->portRect.right - port->portRect.left;
  163.     /* if we are the first INIT to run we need to initialize the horizontal value. */
  164.     if (CheckSum(ShowINITTable[0]) != ShowINITTable[1])
  165.         ShowINITTable[0] = InitialXPosition;
  166.     
  167.     /* compute top left of icon's rect. */
  168.     iconRect->left = (ShowINITTable[0] % screenWidth);
  169.     iconRect->top = port->portRect.bottom - YOffset * (1 + (ShowINITTable[0] / screenWidth));
  170.     iconRect->right = iconRect->left + 32;
  171.     iconRect->bottom = iconRect->top + 32;
  172.     
  173.     /* advance the position for the next icon. */
  174.     ShowINITTable[0] += XOffset;
  175.     
  176.     /* recompute the checksum. */
  177.     ShowINITTable[1] = CheckSum(ShowINITTable[0]);
  178. }
  179.  
  180. /* DrawBWIcon() draws the 'ICN#' member of the icon family. */
  181.  
  182. void DrawBWIcon(short iconId)
  183. {
  184.     Handle icon;
  185.     Rect iconRect;
  186.     BitMap source, destination;
  187.     
  188.     icon = Get1Resource('ICN#', iconId);
  189.     if (!icon)
  190.         return;
  191.     HLock(icon);
  192.     
  193.     GetIconRect(&iconRect);
  194.  
  195.     /* prepare the source and destination bitmaps. */
  196.     source.baseAddr = *icon + 128;                    /* mask address. */
  197.     source.rowBytes = 4;
  198.     SetRect(&source.bounds, 0, 0, 32, 32);
  199.     destination = ((GrafPtr)port)->portBits;
  200.     
  201.     /* transfer the mask. */
  202.     CopyBits(&source, &destination, &source.bounds, &iconRect, srcBic, nil);
  203.     
  204.     /* and the icon. */
  205.     source.baseAddr = *icon;    
  206.     CopyBits(&source, &destination, &source.bounds, &iconRect, srcOr, nil);
  207.     
  208.     ReleaseResource(icon);
  209. }
  210.  
  211. /*
  212.     ChooseIcon() chooses the optimal icon for the current screen depth.
  213.     
  214.     Priorities for choosing icons:
  215.         1. match the bit depth to the icon.
  216.         2. use alternate bit depth version if available.
  217.         3. draw the black & white version.
  218.  */
  219. Handle ChooseIcon(short iconId, short* suggestedDepth)
  220. {
  221.     short depth = *suggestedDepth;
  222.     Handle icon = nil;
  223.  
  224.     if (depth == 4) {
  225.         icon = Get1Resource('icl4', iconId);
  226.         if (!icon) {
  227.             /* try alternate depth. */
  228.             icon = Get1Resource('icl8', iconId);
  229.             if (icon)
  230.                 depth = 8;
  231.         }
  232.     } else {
  233.         depth = 8;
  234.         icon = Get1Resource('icl8', iconId);
  235.         if (!icon) {
  236.             /* try alternate depth. */
  237.             icon = Get1Resource('icl4', iconId);
  238.             if (icon)
  239.                 depth = 4;
  240.         }
  241.     }
  242.     
  243.     *suggestedDepth = depth;
  244.     return icon;
  245. }
  246.  
  247. /* DrawColorIcon() draws the appropriate icon for the current screen depth. */
  248. void DrawColorIcon(short iconId)
  249. {
  250.     short depthToUse;
  251.     Handle mask, icon;
  252.     CTabHandle clut;
  253.     PixMapHandle source;
  254.     BitMap maskBits;
  255.     long rowBytes;
  256.     Rect iconRect, bounds;
  257.     
  258.     /* by default we will be using the actual depth of the screen. */
  259.     depthToUse = theDepth;
  260.     icon = ChooseIcon(iconId, &depthToUse);
  261.     
  262.     /* if no color icon available, draw the black & white icon. */
  263.     if (!icon) {
  264.         DrawBWIcon(iconId);
  265.         return;
  266.     }
  267.     HLock(icon);
  268.     
  269.     /* get the black & white icon to get the mask drawn. */
  270.     mask = Get1Resource('ICN#', iconId);
  271.     if (!mask)
  272.         return;
  273.     HLock(mask);
  274.  
  275.     /* get the correct color lookup table. */
  276.     clut = GetCTable(depthToUse);
  277.     if (!clut)
  278.         return;
  279.     
  280.     /* create a pixmap to stick the icon bits into for screen blitting. */
  281.     source = NewPixMap();
  282.     if (!source) {
  283.         DisposCTable(clut);
  284.         return;
  285.     }
  286.     
  287.     /* set up the source pixmap with the appropriate bounds, depth, and clut. */
  288.     bounds.top = bounds.left = 0;
  289.     bounds.bottom = bounds.right = 32;
  290.     rowBytes = (((depthToUse * 32) + 15) / 16) * 2;
  291.     (**source).baseAddr = *icon;
  292.     (**source).rowBytes = ((short)rowBytes) | 0x8000;
  293.     (**source).bounds = bounds;
  294.     (**source).pixelType = 0;            /* chunky model. */
  295.     (**source).pixelSize = depthToUse;
  296.     (**source).cmpCount = 1;            /* if in 32 bit mode this will be 3, so must change. */
  297.     (**source).cmpSize = depthToUse;    /* only chunky images used. */
  298.     DisposCTable((**source).pmTable);    /* dispose of default, uninitialized table. */
  299.     (**source).pmTable = clut;
  300.  
  301.     /* get position to draw icon in. */
  302.     GetIconRect(&iconRect);
  303.  
  304.     /* prepare the mask bitmap. */
  305.     maskBits.baseAddr = *mask + 128;                    /* mask address. */
  306.     maskBits.rowBytes = 4;
  307.     maskBits.bounds = bounds;
  308.  
  309.     /* punch out the mask. */    
  310.     CopyBits(&maskBits, (BitMapPtr)&port->portPixMap, &bounds, &iconRect, srcBic, (RgnHandle)nil);
  311.     
  312.     /* draw the actual color icon. */
  313.     HLock((Handle)source);
  314.     CopyBits((BitMapPtr)*source, (BitMapPtr)&port->portPixMap, &bounds, &iconRect, srcOr, (RgnHandle)nil);
  315.     
  316.     /* release everything we've allocated. */
  317.     (**source).baseAddr = nil;
  318.     DisposPixMap(source);
  319.     
  320.     /* release the icon and mask. */
  321.     ReleaseResource(icon);
  322.     ReleaseResource(mask);
  323. }
  324.